# coding=utf-8
import os

copy_count = 0


def get_input(tip):
    return input(tip)


def copy_file(src, dest, ccopy_count=None):
    print('src： %s --> dest:%s ' % (src, dest))
    destdir = os.path.dirname(dest)
    if not os.path.exists(destdir):
        os.makedirs(destdir)
    destfilestream = open(dest, 'wb')
    sourcefilestream = open(src, 'rb')
    destfilestream.write(sourcefilestream.read())
    destfilestream.close()
    sourcefilestream.close()
    global copy_count
    copy_count = copy_count + 1


def copy_files(src, dest, suffix):
    if not os.path.exists(src):
        print("copy files . the src is not exists.path:%s", src)
        return
    if os.path.isfile(src):
        print("copy files . the src is not exists.path:%s", src)
        if len(str(suffix)) == 0 or not src.endswith(suffix):
            copy_file(src, dest)
        return
    for f in os.listdir(src):
        sourcefile = os.path.join(src, f)
        targetfile = os.path.join(dest, f)
        if os.path.isfile(sourcefile):
            if len(str(suffix)) > 0:
                if sourcefile.endswith(suffix):
                    copy_file(sourcefile, targetfile)
            else:
                copy_file(sourcefile, targetfile)
        else:
            copy_files(sourcefile, dest, suffix)


input_content = ''
target_content = ''
filter_file_suffix = ''

while not os.path.exists(os.path.join(input_content)):
    if len(str(input_content)) > 0:
        print('拷贝的文件路径不存在')
    input_content = get_input('请输入需要拷贝的文件路径：')

if not os.path.exists(os.path.join(target_content)):
    if len(str(target_content)) > 0:
        open(target_content, "w+", encoding="utf-8")
        if not os.path.exists(os.path.join(target_content)):
            target_content = get_input('请输入拷贝的目标文件路径：')
    else:
        target_content = get_input('请输入拷贝的目标文件路径：')

filter_file_suffix = get_input('拷贝固定后缀文件(默认拷贝全部):')

copy_files(input_content, target_content, filter_file_suffix)
print(' 共拷贝文件%s个 ' % copy_count)
